// pages/round/round.js
const { windOptions, roundOptions, resultOptions, huOptions, timeOptions, markOptions } = require('../../configs/OPTIONS');
const { getRoundInfo, getNextRecord } = require('../../business/records');
const STORAGE_KEYS = require('../../configs/STORAGE_KEYS');
const playersStore = require('../../stores/players');
const recordsStore = require('../../stores/records');
const { omitNilValue } = require('../../utils/util')
const { getTimeValueByTime } = require('../../business/time')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    editRecord: undefined,
    windOptions,
    roundOptions,
    resultOptions,
    huOptions,
    timeOptions,
    markOptions,
    wind: '',
    round: undefined,
    byte: 0,
    playerNums: [],
    gamerOptions: [],
    reachGamers: [],
    result: '',
    winGamer: undefined,
    huMethod: '',
    time: '',
    mark: '',
    tingGamers: [],
    loseGamer: undefined,
  },
  onLoad(option) {
    this.initGamers()
    if (option.key) {
      this.setEditOption(option.key);
    } else {
      this.setNextRecordInfo();
    }
  },
  setEditOption(key) {
    const editRecord = recordsStore.getCurrentRecordByKey(key);
    if (editRecord) {
      const { wind, round, byte, reachGamers, result, winGamer, huMethod, time, mark, tingGamers, loseGamer } = editRecord;
      wx.setNavigationBarTitle({
        title: '编辑' + getRoundInfo(editRecord),
      })
      this.setData(omitNilValue({ editRecord, wind, round, byte, reachGamers, result, winGamer, huMethod, time, mark, tingGamers, loseGamer }))
    }
  },
  /**
   * 校验表单填写是否完整
   */
  validate() {
    let message;
    let gamerValues = this.data.gamerOptions.map(opt => opt.value);
    if (!this.data.wind) {
      message = '请选择场风';
    } else if (!this.data.round) {
      message = '请选择局数'
    } else if (!this.data.result) {
      message = '请选择结局'
    } else if (this.data.result === 'hu') {
      if (!gamerValues.includes(this.data.winGamer)) {
        message = '请选择谁胡'
      } else if (!this.data.huMethod) {
        message = '请选择方式'
      } else if (this.data.huMethod === 'rong') {
        if (!gamerValues.includes(this.data.loseGamer)) {
          message = '请选择谁点'
        } else if (this.data.winGamer === this.data.loseGamer) {
          message = '不能自己点自己'
        }
      }
      // 如果没有报错，则继续做校验
      if (!message) {
        if (this.data.time <= 0) {
          message = '请选择番数'
        } else if (!this.data.mark) {
          message = '请选择符数'
        }
      }
    } else if (this.data.result === 'liu') {
      // 立直的人必定为听牌的人
      const bol = this.data.reachGamers.some(person => !this.data.tingGamers.includes(person));
      if (bol) {
        message = '听牌人中请选择已立直的人'
      }
    }
    if (message) {
      wx.showToast({
        title: message,
        icon: 'none'
      })
      return false;
    }
    return true;
  },
  getFormData() {
    const { wind, round, byte, reachGamers, result, winGamer, huMethod, loseGamer, time, mark, tingGamers } = this.data;
    return { wind, round, byte, reachGamers, result, winGamer, huMethod, loseGamer, time, mark, tingGamers }
  },
  handleSave() {
    if (!this.validate()) {
      return;
    }
    const formData = this.getFormData()
    if (this.data.editRecord) {
      recordsStore.putCurrentGameRecord(formData, this.data.editRecord)
    } else {
      recordsStore.storeCurrentGameRecord(formData)
    }
    wx.navigateBack()
  },
  getReachGamers(evet) {
    this.setData({
      reachGamers: evet.detail,
    })
  },
  setTingGamers(evet) {
    this.setData({
      tingGamers: evet.detail,
    })
  },
  initGamers() {
    const playerNums = wx.getStorageSync(STORAGE_KEYS.CURRENT_PLAYER_NUMS);
    const allPlayers = playersStore.getPlayers()
    const currentPlayers = playerNums.map(playerNum => {
      return allPlayers[playerNum]
    })
    const gamerOptions = currentPlayers.map((player, index) => ({
      label: player.nickname,
      value: index,
    }))
    this.setData({ gamerOptions, playerNums })
  },
  setNextRecordInfo() {
    const playerNums = this.data.playerNums;
    const allCurrentRecords = recordsStore.getCurrentGameRecords();
    const lastRecord = allCurrentRecords[allCurrentRecords.length - 1];
    const nextRecord = getNextRecord(lastRecord, playerNums)
    this.setData({
      wind: nextRecord.wind,
      round: nextRecord.round,
      byte: nextRecord.byte,
    })
  },
  handleGetTime(event) {
    const time = event.detail;
    const timeValue = getTimeValueByTime(time);
    this.setData({
      time: timeValue,
    })
  },
  handleGetMark(event) {
    const mark = event.detail;
    this.setData({ mark: String(mark) })
  }
})